<?php
//Khai bao namespace 
namespace Login\Controller;

//Load lớp AbstractActionController vào CONTROLLER
use Zend\Mvc\Controller\AbstractActionController;
//Load lớp ViewModel vào CONTROLLER
use Zend\View\Model\ViewModel;
use Login\Form\LoginForm;
use Login\Model\Login;

use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Result;
use Zend\Authentication\Storage\Session as SessionStorage;

use Zend\Db\Adapter as DbAdapter;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;
use Zend\Authentication\Validator\Authentication;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $form = new LoginForm();
	    return new ViewModel(array(
	    	'form'=>$form
	    ));
    }
    
    public function registerAction(){	
    	
    	echo '<br/>' . __METHOD__;
    }
    public function loginAction(){
    	$form	=	new LoginForm();
    	$form->get('submit')->setValue('Login');
    	$identity=$message=null;
    	$request	=	$this->getRequest();
    	if($request->isPost()){
    		$authFormFilters	=	new Login();
    		$form->setInputFilter($authFormFilters->getInputFilter());
    		$form->setData($request->getPost());
    		if($form->isValid()){
    			//$config			=	$this->getServiceLocator()->get('Config');
    			$data			= 	$form->getData();
    			$sm=$this->getServiceLocator();
    			$dbAdapter		=	$sm->get('Zend\Db\Adapter\Adapter');    			
    			$AuthAdapter	=	new	AuthAdapter(
	    									$dbAdapter,
    										'users',
    										'email',
    										'password'
    										//,"MD5(?) AND active=1"
    			);
    			$AuthAdapter->setIdentity($data['email'])->setCredential($data['password']);	
    			$auth	=	new AuthenticationService();
    			$result	=	$auth->authenticate($AuthAdapter);
    			
    			//$identity=$this->getServiceLocator()->get('AuthService')->getIdentity();
    			print_r($AuthAdapter->getIdentity());
    			
    			
    			
    			switch ($result->getCode()){
    				case Result::FAILURE_IDENTITY_NOT_FOUND	:
    					break;
    				case Result::FAILURE_CREDENTIAL_INVALID	:
    					break;
    				case Result::SUCCESS	:
    					$storage	=	$auth->getStorage();
    					$storage->write($AuthAdapter->getResultRowObject(null,'password'));
    					$time	=	1209600;
    					if($data['remember-me']){
    						$sessionManager	=	new \Zend\Session\SessionManager();
    						$sessionManager->rememberMe($time);	
    					}
    					break;
    				default:
    					break;
    					
    			}
    			foreach ($result->getMessages() as $message){
    				$message	.=	"$message\n";
    			}
    		}
    	}
    	return new ViewModel(array('form'=>$form,'message'=>$message,'identity' =>$identity ));
    }
    
    public function logoutAction() {
    	$auth	=	new AuthenticationService();
    	if($auth->hasIdentity()){
    		$identity	=	$auth->getIdentity();
    	}
    	$auth->clearIdentity();
    	$sessionManager	=	new	\Zend\Session\SessionManager();
    	$sessionManager->forgetMe();
    	
    	return $this->redirect()->toRoute('login/default',array('controller'=>'index','action'=>'index'));
    }
}

